/*
 * @Description:
 * @Author: duan_shuangjun 丶
 * @Date: 2024-09-03 17:42:25
 * @LastEditTime: 2025-09-13 22:38:14
 */
import { createApp, h } from 'vue'
import MessageTips from './index.vue'

interface MessageOptions {
    title?: string
    content?: string
    isOnCancel?: boolean
    onCancelText?: string
    onSumbitText?: string
}

interface MessageInstance {
    close: () => void
    setLoading: (loading: boolean) => void
}

class ItMessageService {
    private _instance_: any = null
    private _parent_: Element | null = null

    /**
     * @param {MessageOptions} options 消息配置
     * @param {Function} onConfirm 确认回调函数
     * @param {Function} onCancel 取消回调函数
     * @param {Element | string} el 挂载位置，默认挂载在body
     */
    message(options: MessageOptions, onConfirm: () => void, onCancel?: () => void, el?: Element | string): Promise<MessageInstance> {
        return new Promise((resolve, reject) => {
            // 创建容器
            const container = document.createElement('div')

            // 保存this引用
            const self = this

            // 创建Vue应用
            const app = createApp({
                render() {
                    return h(MessageTips, {
                        ...options,
                        onClose: () => {
                            self.close(container, app, reject)
                        },
                        onSumbit: () => {
                            // 用户点击确定时，执行回调函数
                            onConfirm()
                        },
                        onCancel: () => {
                            // 用户点击取消时，执行取消回调函数
                            if (onCancel) {
                                onCancel()
                            } else {
                                self.close(container, app, reject)
                            }
                        },
                        onManualClose: () => {
                            self.manualClose(container, app)
                        }
                    })
                }
            })

            // 挂载应用
            app.mount(container)

            // 确定挂载位置
            if (!el) {
                this._parent_ = document.body
            } else if (this.isHTMLElement(el)) {
                this._parent_ = el as Element
            } else {
                this._parent_ = document.querySelector(el as string)
            }

            if (this._parent_) {
                this._parent_.appendChild(container)
            }

            // 立即返回组件实例
            const messageInstance: MessageInstance = {
                close: () => self.close(container, app),
                setLoading: (loading: boolean) => {
                    // 通过全局变量获取组件实例
                    if (window.__messageTipsInstance__) {
                        window.__messageTipsInstance__.setLoading(loading)
                    }
                }
            }
            resolve(messageInstance)
        })
    }

    private close(container: Element, app: any, reject?: Function) {
        if (this._parent_) {
            this._parent_.removeChild(container)
        }
        app.unmount()
        if (reject) {
            reject('用户取消')
        }
    }

    private manualClose(container: Element, app: any) {
        if (this._parent_) {
            this._parent_.removeChild(container)
        }
        app.unmount()
    }

    private isHTMLElement(obj: any): boolean {
        return obj && obj.nodeType && obj.nodeType === 1
    }
}

// 声明全局变量
declare global {
    interface Window {
        __messageTipsInstance__?: any
    }
}

export default new ItMessageService()
